package com.blackhorse.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.blackhorse.service.impl.OrderServiceImpl3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试sentinel的controller
 * 使用时打开RestController注解
 */
@Slf4j
//@RestController
public class OrderController3 {

    @Autowired
    private OrderServiceImpl3 orderServiceImpl3;

    @RequestMapping("/order/message1")
    public String message1() {
        //在不同的接口调用message方法，用于测试流控链路规则
        orderServiceImpl3.message();
        return "测试message1";
    }

    private int i = 0;

    @RequestMapping("/order/message2")
    public String message2() {
        i++;
        //模拟异常比例1/3
        if (i % 3 == 0) {
            throw new RuntimeException("模拟异常");
        }
        orderServiceImpl3.message();
        return "测试message2";
    }

    /**
     * 测试热点规则，必须使用SentinelResource注解
     */
    @RequestMapping("/order/message3")
    @SentinelResource("message3")
    public String message3(String name, Integer age) {
        return "测试message3：" + name + "," + age;
    }

    @RequestMapping("/order/message4")
    public String message4() {
        return orderServiceImpl3.message();
    }
}
